<?php


namespace app\controller\archives;
require_once __DIR__.'/../../common/AuthController.php';
use app\common\AuthController;


/**
 * 运营中心
 */
class Itoc extends AuthController
{   


    /**
     * 初始化操作
     */
    public function init()
    {

    }
	

    /**
     * 获取七大区
     */
	public function api_archives_itoc_sevenRegional($msg)
    {   
        global $db;
        $info = [
            'id' => isset($msg['data']['id'])?$msg['data']['id']:'',
            'name' => isset($msg['data']['name'])?$msg['data']['name']:'',
            'linkman' => isset($msg['data']['linkman'])?$msg['data']['linkman']:'',
            'linkman_mobile' => isset($msg['data']['linkman_mobile'])?$msg['data']['linkman_mobile']:'',
        ];
        $where = "is_delete = 0";
        if(!empty($info['id'])){
            $where .= " AND id =".$info['id'];
        }
        if(!empty($info['name'])){
            $where .= " AND name LIKE '%".$info['name']."%'";
        }
        if(!empty($info['linkman'])){
            $where .= " AND linkman LIKE '%".$info['linkman']."%'";
        }
        if(!empty($info['linkman_mobile'])){
            $where .= " AND linkman_mobile LIKE '%".$info['linkman_mobile']."%'";
        }
        $list = $db->query("select * from admin_region where ".$where); 
        $count =  $db->query("SELECT count(*) as count from admin_region WHERE ".$where);
        unset($list[0]);
        $list = array_values($list);
        return $this->outMessage(10000,'获取成功',$list,'success',$count[0]['count']-1);
	}




    /**
     * 获取运营中心
     */
    public function api_archives_itoc_itocList($msg)
    {   
        global $db;
        $info = [
            'page' =>  isset($msg['data']['page'])?$msg['data']['page']:1,
            'rows' => isset($msg['data']['rows'])?$msg['data']['rows']:15,
            'id' => isset($msg['data']['id'])?$msg['data']['id']:'',
            'admin_region_name' => isset($msg['data']['admin_region_name'])?$msg['data']['admin_region_name']:'',
            'region_name' => isset($msg['data']['region_name'])?$msg['data']['region_name']:'',
            'name' => isset($msg['data']['name'])?$msg['data']['name']:'',
            'linkman' => isset($msg['data']['linkman'])?$msg['data']['linkman']:'',
            'linkman_mobile' => isset($msg['data']['linkman_mobile'])?$msg['data']['linkman_mobile']:'',
        ];
        $limit = ($info['page']-1)*$info['rows'].','.$info['rows'];
        $where = "is_delete = 0";
        if(!empty($info['id'])){
            $where .= " AND id =".$info['id'];
        }
        if(!empty($info['name'])){
            $where .= " AND name LIKE '%".$info['name']."%'";
        }
        if(!empty($info['admin_region_name'])){
            $where .= " AND admin_region_name LIKE '%".$info['admin_region_name']."%'";
        }
        if(!empty($info['linkman'])){
            $where .= " AND linkman LIKE '%".$info['linkman']."%'";
        }
        if(!empty($info['linkman_mobile'])){
            $where .= " AND linkman_mobile LIKE '%".$info['linkman_mobile']."%'";
        }
        if(!empty($info['region_name'])){
            $where .= " AND region_name LIKE '%".$info['region_name']."%'";
        }
        $list = $db->query("select * from itoc where ".$where ." order by id  desc limit ".$limit); 
        $count =  $db->query("SELECT count(*) as count from itoc WHERE ".$where);
        return $this->outMessage(10000,'获取成功',$list,'success',$count[0]['count']);
    }




    /**
     * 运营中心禁用启用
     */
    public function api_archives_itoc_editStatus($msg)
    {
        $status = isset($msg['data']['status'])?$msg['data']['status']:'';
        $id = isset($msg['data']['id'])?$msg['data']['id']:'';
        if(!$id || !$status){
            return $this->outMessage(-1,'参数不能为空');
        }
        if($status > 2 || $status < 1){
            return $this->outMessage(-1,'参数不正确');
        }
        $res = $this->table->update('itoc',['id'=>$id],['status'=>$status,'update_time'=>date('Y-m-d H:i:s',time())]);
        if(!$res){
            if($status == 1){
                $error = '启用失败';
            }else{
                $error = '禁用失败';
            }
            return $this->outMessage(-1,$error);
        }
        if($status == 1){
            $success = '启用成功';
        }else{
            $success = '禁用成功';
        }
        return $this->outMessage(10000,$success);
    }



    /**
     * 添加
     */
    public function api_archives_itoc_add($msg)
    {
        $info = [
            'name' => isset($msg['data']['name'])?$msg['data']['name']:'',
            'admin_region_id' => isset($msg['data']['admin_region_id'])?$msg['data']['admin_region_id']:'',
            'admin_region_name' => isset($msg['data']['admin_region_name'])?$msg['data']['admin_region_name']:'',
            'linkman' => isset($msg['data']['linkman'])?$msg['data']['linkman']:'',
            'linkman_mobile' => isset($msg['data']['linkman_mobile'])?$msg['data']['linkman_mobile']:'',
            'region_id' => isset($msg['data']['region_id'])?$msg['data']['region_id']:'',
            'region_name' => isset($msg['data']['region_name'])?$msg['data']['region_name']:'',
            'bank_account_name' => isset($msg['data']['bank_account_name'])?$msg['data']['bank_account_name']:'',
            'bank_account_number' => isset($msg['data']['bank_account_number'])?$msg['data']['bank_account_number']:'',
            'bank_name' => isset($msg['data']['bank_name'])?$msg['data']['bank_name']:'',
            'bank_address' => isset($msg['data']['bank_address'])?$msg['data']['bank_address']:'',
            'deadline' => isset($msg['data']['deadline'])?$msg['data']['deadline']:''
        ];
        foreach ($info as $v) {
            if(empty($v)){
                return $this->outMessage(-1,'参数不能为空');
            }
        }
        if($info['deadline'] < 1){
            return $this->outMessage(-1,'有效年限不能低于一年');
        }
        if(!$this->validate->checkName($info['name'])){
            return $this->outMessage(-1,'请输入正确的名称');
        }
        if(!$this->validate->checkName($info['linkman'])){
            return $this->outMessage(-1,'请输入正确的联系人');
        }
        if(!$this->validate->checkMobile($info['linkman_mobile'])){
            return $this->outMessage(-1,'请输入正确的联系电话');
        }
        if(!$this->validate->checkName($info['bank_account_name'],2,10)){
            return $this->outMessage(-1,'请输入正确的银行开户名');
        }
        if(!is_numeric($info['bank_account_number'])){
            return $this->outMessage(-1,'请输入正确的银行卡号');
        }
        if(!$this->validate->checkBankCard($info['bank_account_number'])){
            return $this->outMessage(-1,'请输入正确的银行卡号');
        }
        if(!$this->validate->checkName($info['bank_name'],2,10)){
            return $this->outMessage(-1,'请输入正确的开户银行支行名称');
        }
        if(!$this->validate->checkName($info['bank_address'],2,50)){
            return $this->outMessage(-1,'请输入正确的开户银行所在地');
        }
        $res = $this->table->find('itoc',['region_id'=>$info['region_id']]);
        if($res){
            return $this->outMessage(-1,'该地区已有运营中心存在');
        }
        $res = $this->table->find('itoc',['linkman_mobile'=>$info['linkman_mobile']]);
        if($res){
            return $this->outMessage(-1,'该联系人电话存在,不可重复添加.');
        }
        $data = [
            'admin_region_id' => $info['admin_region_id'],
            'admin_region_name' => $info['admin_region_name'],
            'name' => $info['name'],
            'linkman' => $info['linkman'],
            'linkman_mobile' => $info['linkman_mobile'],
            'region_id' => $info['region_id'],
            'region_name' => $info['region_name'],
            'bank_account_name' => $info['bank_account_name'],
            'bank_account_number' => $info['bank_account_number'],
            'bank_name' => $info['bank_name'],
            'bank_address' => $info['bank_address'],
            'deadline' => $info['deadline'],
            'create_time' => date('Y-m-d H:i:s',time()),
            'expire_time' => date('Y-m-d H:i:s',time()+$info['deadline']*31536000),
        ];
        $res = $this->table->insert('itoc',$data);
        if(!$res){
            return $this->outMessage(-1,'添加失败');
        }
        return $this->outMessage(10000,'添加成功');
    }



    /**
     * 编辑
     */
    public function api_archives_itoc_edit($msg)
    {
        $info = [
            'id' => isset($msg['data']['id'])?$msg['data']['id']:'',
            'name' => isset($msg['data']['name'])?$msg['data']['name']:'',
            'linkman' => isset($msg['data']['linkman'])?$msg['data']['linkman']:'',
            'linkman_mobile' => isset($msg['data']['linkman_mobile'])?$msg['data']['linkman_mobile']:'',
            'bank_account_name' => isset($msg['data']['bank_account_name'])?$msg['data']['bank_account_name']:'',
            'bank_account_number' => isset($msg['data']['bank_account_number'])?$msg['data']['bank_account_number']:'',
            'bank_name' => isset($msg['data']['bank_name'])?$msg['data']['bank_name']:'',
            'bank_address' => isset($msg['data']['bank_address'])?$msg['data']['bank_address']:'',
        ];
        foreach ($info as $v) {
            if(empty($v)){
                return $this->outMessage(-1,'参数不能为空');
            }
        }
        if(!$this->validate->checkName($info['name'])){
            return $this->outMessage(-1,'请输入正确的名称');
        }
        if(!$this->validate->checkName($info['linkman'])){
            return $this->outMessage(-1,'请输入正确的联系人');
        }
        if(!$this->validate->checkMobile($info['linkman_mobile'])){
            return $this->outMessage(-1,'请输入正确的联系电话');
        }
        if(!$this->validate->checkName($info['bank_account_name'],2,10)){
            return $this->outMessage(-1,'请输入正确的银行开户名');
        }
        if(!is_numeric($info['bank_account_number'])){
            return $this->outMessage(-1,'请输入正确的银行卡号');
        }
        if(!$this->validate->checkBankCard($info['bank_account_number'])){
            return $this->outMessage(-1,'请输入正确的银行卡号');
        }
        if(!$this->validate->checkName($info['bank_name'],2,10)){
            return $this->outMessage(-1,'请输入正确的开户银行支行名称');
        }
        if(!$this->validate->checkName($info['bank_address'],2,50)){
            return $this->outMessage(-1,'请输入正确的开户银行所在地');
        }
        $res = $this->table->find('itoc',['linkman_mobile'=>$info['linkman_mobile']]);
        if(!empty($res) && $res['id'] != $info['id']){
            return $this->outMessage(-1,'该联系人电话存在,不可重复添加.');
        }
        $data = [
            'name' => $info['name'],
            'linkman' => $info['linkman'],
            'linkman_mobile' => $info['linkman_mobile'],
            'bank_account_name' => $info['bank_account_name'],
            'bank_account_number' => $info['bank_account_number'],
            'bank_name' => $info['bank_name'],
            'bank_address' => $info['bank_address'],
            'update_time' => date('Y-m-d H:i:s',time())
        ];
        $res = $this->table->update('itoc',['id'=>$info['id']],$data);
        if(!$res){
            return $this->outMessage(-1,'保存失败');
        }
        return $this->outMessage(10000,'保存成功');
    }

}
